package main

import (
	"awesomeProject3/middlewares"
	"awesomeProject3/models"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func main() {
	r := gin.Default()
	r.GET("/mm", func(ctx *gin.Context) {
		j := middlewares.NewJWT()
		claims := models.CustomClaims{
			ID:          uint(123),
			NickName:    "张三",
			AuthorityId: uint(2), //这个我用来识别管理员和普通用户
			StandardClaims: jwt.StandardClaims{
				NotBefore: time.Now().Unix(),               //签名的生效时间
				ExpiresAt: time.Now().Unix() + 60*60*24*30, //30天过期
				Issuer:    "nihao",
			},
		}
		token, err := j.CreateToken(claims)
		if err != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"msg": "生成token失败",
			})
			return
		}

		ctx.JSON(http.StatusOK, gin.H{
			"id":          "123",
			"nick_name":   "张三",
			"token":       token,
			"expiresd_at": (time.Now().Unix() + 60*60*24*30) * 1000,
		})
	})
	//验证token  添加中间件
	r.GET("/tok", middlewares.JWTAuth(), middlewares.IsAdminAuth(), func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{
			"msg": "ok",
		})
	})

	_ = r.Run(":8056")

}
